//
//  SwiftUIView_Initialize.swift
//  SwiftUIPractice
//
//  Created by 音乐 (external - Project) on 2024/9/9.
//

import SwiftUI

struct SwiftUIView_Initialize: View {
    
    //开始定义属性
    let backgroundColor: Color

    //可以使用默认的值来初始化这个属性
    //let backgroundColor: Color = .red
    
    let count: Int
    let title: String


    //在含有未初始化的属性 和 没有写 初始化函数的时候, SwiftUI 默认会给出这样的初始化函数, 不需要写,
//    init(backgroundColor: Color, count: Int, title: String) {
//        self.backgroundColor = backgroundColor
//        self.count = count
//        self.title = title
//    }
  
    
    init(count: Int, fruit: Fruit) {
        self.count = count
        if fruit == .Apple {
            self.title = "Apples"
            self.backgroundColor = .red
        } else {
            self.title = "Oranges"
            self.backgroundColor = .orange
        }
    }

    enum Fruit {
        case Apple
        case Oranges
    }
    
    
    var body: some View {
        
        VStack(spacing: 12) {
            
            Text("\(count)").font(.largeTitle).foregroundStyle(.white).underline()
            Text(title).font(.headline).foregroundStyle(.white)
            
        }
        .frame(width: 150, height: 150)
        .background(backgroundColor)
        .cornerRadius(10)
        
    }
}

#Preview {
    //Swift里面的属性要求初始化,
    
    //SwiftUIView_Initialize(backgroundColor: .orange, count: 55, title: "Oranges")
    
    //SwiftUIView_Initialize(count: 99, fruit: .Apple)
    

    //在这个预览里面, 也是可以写这种 SwiftUI 代码的
    HStack {
        SwiftUIView_Initialize(count: 99, fruit: .Apple)

        SwiftUIView_Initialize(count: 10, fruit: .Oranges)
        
    }
    
}
